package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FlowPackageAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.CalculatePkgUsageUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.domain.inst.repository.SceneInstRepository;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName : LocalProvinceAttrLinkHandler
 * @Description : 流量套包属性联动处理类，流量套包联动套内用量和套包价格
 * @Author : xie.zhengjun
 * @Date : 2020/9/22 17:12
 */
@Service
@AttrHandler(FlowPackageAttrHandler.class)
public class FlowPackageAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //获取specID用来区分类别,是5G还是4G等。
        String specID = inst.getSpecId();
        //获取流量套包的值
        String flowPackage = attr.getValue();
        //获取套内用量属性信息
        KiteAttr packageUsage = inst.getAttr("200004083");
        KiteAttr kiteAttr = new KiteAttr();
        kiteAttr.setAttrValueTypeId("98A");
        kiteAttr.setIsNull("F");
        //获取规格ID
        KiteOfferInst offerInst = querySceneInstService.getMainOfferInst(data.getSceneInstId());
        String offerId = offerInst.getOfferId();

        if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equals(specID,"40000017013")){
            // 5G通用连接产品, 流量套包联动套内用量
            if(KiteStringUtils.isNotBlank(flowPackage) && "100007194".equals(flowPackage)){
                //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                kiteAttr.setAttrId("202216546");//配置的月的
                doSetAttrValues(kiteAttr,packageUsage,"100007222","100007222","30GB",inst,data);
            }else if(KiteStringUtils.isNotBlank(flowPackage) && "100007195".equals(flowPackage)){
                //如果流量套包选择的是季包，则套内用量联动展示月包的套内用量*3
                kiteAttr.setAttrId("202106151436");//配置的季的
                doSetAttrValues(kiteAttr,packageUsage,"110007706","110007706","90GB",inst,data);
            }else if(KiteStringUtils.isNotBlank(flowPackage) && "100007196".equals(flowPackage)){
                //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                kiteAttr.setAttrId("202106151446");//配置的年的
                doSetAttrValues(kiteAttr,packageUsage,"110007724","110007724","360GB",inst,data);
            }

            //5G纯数据的联动为 流量套包联动通信套包类型
            //选择月包时，通信套包类型只能选择月付
            //选择季包、年包时，通信套包类型只能选择预付
            if("50040701".equals(offerId)){
                KiteAttr feePkgTypeAttr = inst.getAttr("200003697");
                if(Objects.nonNull(feePkgTypeAttr)){
                    doSetValueByFlowPackage(flowPackage,feePkgTypeAttr,data,inst);
                }
                KiteAttr baseUsageAttr = inst.getAttr("200003701");
                //同时，【通信套包类型】联动【根据用量终止期限】，通信套包类型选择预付时展示且必填（即流量套包为年包或季包时展示或必填）
                if(KiteStringUtils.isNotBlank(flowPackage) && KiteStringUtils.equalsAny(flowPackage,"100007195","100007196")){
                    if (Objects.nonNull(baseUsageAttr)) {
                        baseUsageAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), baseUsageAttr);

                    }
                }else {
                    if (Objects.nonNull(baseUsageAttr)) {
                        baseUsageAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), baseUsageAttr);
                    }
                }
            }
        }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equalsAny(specID,"30000017015", "30000017018", "30000017020")){
            CalculatePkgUsageUtil.calculatePkgUsage(attr, inst, data);
            // 自动关联，年包预付单个，月包月付灵活
            KiteAttr feePkgTypeAttr = inst.getAttr("200003697");
            if (KiteStringUtils.equalsAny(specID,"30000017018", "30000017020") && Objects.nonNull(feePkgTypeAttr)) {
                if(KiteStringUtils.isNotBlank(flowPackage) && "100007194".equals(flowPackage)){
                    List<KiteAttrValue> attrValues = SceneInstRepository.getKiteAttrValues(feePkgTypeAttr).stream().filter(val -> KiteStringUtils.equalsAny(val.getAttrValue(), "100006432")).collect(Collectors.toList());
                    if (KiteListUtils.isNotEmpty(attrValues)) {
                        feePkgTypeAttr.setValue(attrValues.get(0).getAttrValue());
                        feePkgTypeAttr.setValueDesc(attrValues.get(0).getAttrValueName());
                        feePkgTypeAttr.setAttrValues(attrValues);
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), feePkgTypeAttr);
                        LocalAttrHelper.hideAndNullable(inst, "200003701");
                    }
                } else if(KiteStringUtils.isNotBlank(flowPackage) && "100007196".equals(flowPackage)){
                    List<KiteAttrValue> attrValues = SceneInstRepository.getKiteAttrValues(feePkgTypeAttr).stream().filter(val -> KiteStringUtils.equals(val.getAttrValue(), "100006433")).collect(Collectors.toList());
                    if (KiteListUtils.isNotEmpty(attrValues)) {
                        feePkgTypeAttr.setValue(attrValues.get(0).getAttrValue());
                        feePkgTypeAttr.setValueDesc(attrValues.get(0).getAttrValueName());
                        feePkgTypeAttr.setAttrValues(attrValues);
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), feePkgTypeAttr);
                        LocalAttrHelper.showAndNotNull(inst, "200003701");
                    }
                }
            }
        }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equals(specID,"30000017014")){
            //NB通用连接产品, 流量套包联动套内用量
            if(KiteStringUtils.isNotBlank(flowPackage) && "100007194".equals(flowPackage)){
                //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                kiteAttr.setAttrId("201218546");//配置的月的
                doSetAttrValues(kiteAttr,packageUsage,"100007216","100007216","10MB",inst,data);
            }else if(KiteStringUtils.isNotBlank(flowPackage) && "100007196".equals(flowPackage)){
                //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                kiteAttr.setAttrId("202106151642");//配置的年的
                doSetAttrValues(kiteAttr,packageUsage,"110007688","110007688","120MB",inst,data);
            }
        }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equalsAny(specID,"30000017013", "30000017019")){
            //4G通用连接产品 ,流量套包联动套内用量
            if(KiteStringUtils.isNotBlank(flowPackage) && "100007194".equals(flowPackage)){
                //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                kiteAttr.setAttrId("201216546");//配置的月的
                doSetAttrValues(kiteAttr,packageUsage,"100007200","100007200","30MB",inst,data);
            }else if(KiteStringUtils.isNotBlank(flowPackage) && "100007195".equals(flowPackage)){
                //如果流量套包选择的是季包，则套内用量联动展示月包的套内用量*3
                kiteAttr.setAttrId("202106151651");//配置的季的
                doSetAttrValues(kiteAttr,packageUsage,"110007687","110007687","90MB",inst,data);
            }else if(KiteStringUtils.isNotBlank(flowPackage) && "100007196".equals(flowPackage)){
                //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                kiteAttr.setAttrId("202106151713");//配置的年的
                doSetAttrValues(kiteAttr,packageUsage,"110007708","110007708","360MB",inst,data);
            }

            //4G纯数据的联动为 流量套包联动通信套包类型
            //选择月包时，通信套包类型只能选择月付
            //选择季包、年包时，通信套包类型只能选择预付
            if("50040702".equals(offerId)){
                KiteAttr feePkgTypeAttr = inst.getAttr("200003697");
                if(Objects.nonNull(feePkgTypeAttr)){
                    doSetValueByFlowPackage(flowPackage,feePkgTypeAttr,data,inst);
                }
            }
        }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equals(specID,"30000017017")){
            //融合类CAT1通用连接产品,流量套包联动套内用量
            if(KiteStringUtils.isNotBlank(flowPackage) && "100007194".equals(flowPackage)){
                //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                kiteAttr.setAttrId("2021041440");//配置的月的
                doSetAttrValues(kiteAttr,packageUsage,"100007200","100007200","30MB",inst,data);
            }else if(KiteStringUtils.isNotBlank(flowPackage) && "100007196".equals(flowPackage)){
                //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                kiteAttr.setAttrId("202106151730");//配置的年的
                doSetAttrValues(kiteAttr,packageUsage,"110007708","110007708","360MB",inst,data);
            }
        }

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void doSetAttrValues(KiteAttr kiteAttr,KiteAttr packageUsage,String defaultValue,String value,String valueDesc,KiteInst inst, AttrData data){
        // 流量套包联动展示套内用量
        List<KiteAttrValue> need = SceneInstRepository.getKiteAttrValues(kiteAttr);
        packageUsage.setDefaultValue(defaultValue);
        packageUsage.setValue(value);
        packageUsage.setValueDesc(valueDesc);
        packageUsage.setAttrValues(need);
        // 流量套包联动展示套包价格
        if(!KiteStringUtils.isEmpty(packageUsage.getValue())){
            String code = DcPublicCache.getCodea("202012180001",packageUsage.getValue());
            if(!KiteStringUtils.isEmpty(code)){
                operateInstAttrService.setAttrValue(inst, "200003704", code, data.getSceneInstId(), null);
            }
        }
    }
    //根据套包类型设置月付或预付
    private void doSetValueByFlowPackage(String flowPackage, KiteAttr feePkgTypeAttr, AttrData data, KiteInst inst){
        if(KiteStringUtils.isNotBlank(flowPackage) && "100007194".equals(flowPackage)){
            //选择月包时，通信套包类型只能选择月付灵活共享
            List<KiteAttrValue> attrValues = SceneInstRepository.getKiteAttrValues(feePkgTypeAttr).stream().filter(val -> KiteStringUtils.equals(val.getAttrValue(), "100006431")).collect(Collectors.toList());
            if (KiteListUtils.isNotEmpty(attrValues)) {
                feePkgTypeAttr.setValue(attrValues.get(0).getAttrValue());
                feePkgTypeAttr.setValueDesc(attrValues.get(0).getAttrValueName());
                feePkgTypeAttr.setAttrValues(attrValues);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), feePkgTypeAttr);
                LocalAttrHelper.hideAndNullable(inst, "200003701");
            }
        } else if(KiteStringUtils.isNotBlank(flowPackage) && KiteStringUtils.equalsAny(flowPackage,"100007195","100007196")){
            //选择季包、年包时，通信套包类型只能选择预付单个连接
            List<KiteAttrValue> attrValues = SceneInstRepository.getKiteAttrValues(feePkgTypeAttr).stream().filter(val -> KiteStringUtils.equals(val.getAttrValue(), "100006433")).collect(Collectors.toList());
            if (KiteListUtils.isNotEmpty(attrValues)) {
                feePkgTypeAttr.setValue(attrValues.get(0).getAttrValue());
                feePkgTypeAttr.setValueDesc(attrValues.get(0).getAttrValueName());
                feePkgTypeAttr.setAttrValues(attrValues);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), feePkgTypeAttr);
                LocalAttrHelper.showAndNotNull(inst, "200003701");
            }
        }
    }
}
